package com.ftg.learn.mapper;

import com.ftg.learn.enity.SysResources;
import com.ftg.learn.enity.SysUser;
import com.ftg.learn.util.ResultJson;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.io.Resources;

import java.util.List;
import java.util.Map;

/**
 * user表操作
 * @author wangty
 * @date 2022/10/20
 */
@Mapper
public interface UserMapper {

    /**
     *用当前用户查询权限
     * @param u_id
     * @return
     */
    @Select("select distinct * " +
            "from sys_resources " +
            "where id in (select res_id " +
            "             from sys_role_resources " +
            "             where r_id in ( " +
            "                 select r_id " +
            "                 from sys_user_role " +
            "                 where u_id = #{uid} and is_deleted = 0 " +
            "             ) " +
            "               and is_deleted = 0) " +
            "  and is_deleted = 0;")
     List<SysResources> showPrivilegeWithUid(Integer u_id) ;

    /**
     * 用来判断当前用户的账号与密码是否存在
     * @param u_account 账号
     * @param u_password 密码
     * @return
     */
    @Select("select * from sys_user where is_deleted = 0 and u_account=#{u_account} and u_password=#{u_password}")
    SysUser loginForUser(
            @Param("u_account") String u_account,
            @Param("u_password") String u_password);

    /**
     * 员工表
     * @param emp_name 姓名
     * @param emp_sex 性别
     * @param emp_code 学号
     * @param emp_type 类型
     * @param emp_job 岗位
     * @param emp_birthday  生日
     * @param clazz_id 班级
     * @return
     */
    @Insert("insert into sys_employee(emp_name,emp_sex,emp_code,emp_type,emp_job,emp_birthday,clazz_id)" +
            "values(#{emp_name},#{emp_sex},#{emp_code},#{emp_type},#{emp_job},#{emp_birthday},#{clazz_id})")
    int creatEmployee(@Param("emp_name") String emp_name,@Param("emp_sex")String emp_sex,
                      @Param("emp_code")String emp_code,
                      @Param("emp_type")Integer emp_type,@Param("emp_job")String emp_job,
                      @Param("emp_birthday")String emp_birthday,@Param("clazz_id")Integer clazz_id);

    /**
     * 创建用户
     * @param sysUser User实例
     * @return
     */
    @Insert("insert into sys_user(u_account,u_password,e_id)" +
            "values(#{sysUser.u_account},#{sysUser.u_password},#{sysUser.e_id})")
    int creatUser(@Param("sysUser") SysUser sysUser);

    /**
     * 用来查询员工表里是否与用户创建的账号重复
     * 查询最大id
     * @return
     */
    @Select("select max(id) max from sys_employee")
    Map<String, Integer> selectEmployee();

    /**
     * 忘记密码，重置密码
     * @param sysUser User实例
     * @return
     */
    @Update("update")
    int changePassword(SysUser sysUser);
}
